home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWResour / Sources / FWResAcc.cpp < prev    next >
Encoding:
Text File  |  1996-04-25  |  9.3 KB  |  310 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWResAcc.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef   FWRESACC_H
  13. #include "FWResAcc.h"
  14. #endif
  15.  
  16.  
  17. #ifdef FW_BUILD_MAC
  18. #pragma segment fwresour
  19. #endif
  20.  
  21. #if defined(__MWERKS__) && GENERATING68K
  22. // A hack to work around a bug
  23. #pragma import list somNewObjectInstance
  24. #endif
  25.  
  26. //========================================================================================
  27. //    Template instantiation
  28. //========================================================================================
  29.  
  30. #include "FWSOMPtr.tpp"
  31.  
  32. FW_DEFINE_AUTO_TEMPLATE(FW_TCountedSOMPtr, FW_OResource)
  33.  
  34. #if FW_USE_TEMPLATE_PRAGMAS
  35. #pragma template_access public
  36. #pragma template FW_TCountedSOMPtr<FW_OResource>
  37. #endif
  38.  
  39. #if FW_ANSI_TEMPLATE_INTANTIATION
  40. template class FW_TCountedSOMPtr<FW_OResource>;
  41. #endif
  42.  
  43. //========================================================================================
  44. // CLASS FW_PResource
  45. //========================================================================================
  46.  
  47. FW_DEFINE_AUTO(FW_PResource)
  48.  
  49. //----------------------------------------------------------------------------------------
  50. // FW_PResource::FW_PResource
  51. //----------------------------------------------------------------------------------------
  52.  
  53. FW_PResource::FW_PResource(Environment* ev,
  54.                            FW_OResourceFile* file,
  55.                            FW_ResourceId resourceId,
  56.                            FW_ResourceType resourceType)
  57. {
  58.     FW_OResource* rep = new FW_OResource();
  59.  
  60.     SetRep(ev, rep);
  61.     rep->InitFromFile(ev,
  62.                       file, 
  63.                       resourceId, 
  64.                       resourceType);
  65.     FW_END_CONSTRUCTOR
  66. }
  67.  
  68. //----------------------------------------------------------------------------------------
  69. // FW_PResource::FW_PResource
  70. //----------------------------------------------------------------------------------------
  71.  
  72. FW_PResource::FW_PResource(const FW_PResource& other) :
  73.     FW_TCountedSOMPtr<FW_OResource>(other)
  74. {
  75.     FW_END_CONSTRUCTOR
  76. }
  77.  
  78. //----------------------------------------------------------------------------------------
  79. // FW_PResource::FW_PResource
  80. //----------------------------------------------------------------------------------------
  81.  
  82. FW_PResource::FW_PResource(Environment* ev, FW_OResource* other) :
  83.     FW_TCountedSOMPtr<FW_OResource>(ev, other)
  84. {
  85.     FW_END_CONSTRUCTOR
  86. }
  87.  
  88. //----------------------------------------------------------------------------------------
  89. // FW_PResource::~FW_PResource
  90. //----------------------------------------------------------------------------------------
  91.  
  92. FW_PResource::~FW_PResource()
  93. {
  94.     FW_START_DESTRUCTOR
  95. }
  96.  
  97. //========================================================================================
  98. //    CLASS FW_CAcquireResourceData
  99. //
  100. //    A resource data acquistion helper object.  This object can be used in order
  101. //    to obtain access to resource data in an exception safe manner.
  102. //========================================================================================
  103.  
  104. FW_DEFINE_AUTO(FW_CAcquireResourceData)
  105.  
  106. //----------------------------------------------------------------------------------------
  107. // FW_CAcquireResourceData::FW_CAcquireResourceData
  108. //----------------------------------------------------------------------------------------
  109.  
  110. FW_CAcquireResourceData::FW_CAcquireResourceData(Environment *ev, FW_OResource* resource) :
  111.     fResource(ev, resource)
  112. {
  113.     fData = fResource->AcquireData(ev);
  114.     fSize = fResource->GetSize(ev);
  115.     FW_END_CONSTRUCTOR
  116. }
  117.  
  118. //----------------------------------------------------------------------------------------
  119. // FW_CAcquireResourceData::~FW_CAcquireResourceData
  120. //----------------------------------------------------------------------------------------
  121.  
  122. FW_CAcquireResourceData::~FW_CAcquireResourceData()
  123. {
  124.     FW_START_DESTRUCTOR
  125.     FW_SOMEnvironment ev;
  126.  
  127.     fResource->ReleaseData(ev);
  128. }
  129.  
  130. //========================================================================================
  131. //    Global Utility Functions
  132. //========================================================================================
  133.  
  134. #ifdef FW_BUILD_WIN
  135. #pragma pack(push,1)
  136. #endif
  137.  
  138. struct FW_SMultiStringResEntry
  139. {
  140.     short fStringId;
  141.     unsigned short fStringOffset;
  142. };
  143.  
  144. struct FW_SMultiStringHeader
  145. {
  146.     unsigned short fStringCount;                // number of entrys
  147.     FW_SMultiStringResEntry fEntryArray[1];        // table of entries
  148.     unsigned short GetStringOffset(short theKey);
  149. };
  150.  
  151. #ifdef FW_BUILD_WIN
  152. #pragma pack(pop)
  153. #endif
  154.  
  155. //----------------------------------------------------------------------------------------
  156. // FW_SMultiStringHeader::GetStringOffset
  157. //----------------------------------------------------------------------------------------
  158.  
  159. static const unsigned short kBadOffset = 0xFFFF;
  160.  
  161. unsigned short FW_SMultiStringHeader::GetStringOffset(short theKey)
  162. {
  163.     short wLo = 0,
  164.     wHi = fStringCount - 1;
  165.     short wValue;                                // trial Value
  166.     // X[wLo..wHi] are ordered, but untested
  167.     do
  168.     {
  169.         short wMid = (wLo + wHi) / 2;
  170.         wValue = fEntryArray[wMid].fStringId;    // try middle Value
  171.         if (wValue <= theKey)
  172.             wLo = wMid + 1;                // X[0..wLo-1] <= theKey
  173.  
  174.         if (wValue >= theKey)
  175.             wHi = wMid - 1;                // X[wHi+1..wMax] >= theKey  X[wLo..wHi] untested
  176.     } while (wLo <= wHi);                // still at least one untested
  177.  
  178.     if (wLo - wHi == 2)
  179.         return fEntryArray[wHi + 1].fStringOffset;
  180.  
  181.     return kBadOffset;
  182. }
  183.  
  184. //========================================================================================
  185. //    Global Functions
  186. //========================================================================================
  187.  
  188. //----------------------------------------------------------------------------------------
  189. // FW_HasStringByPosition
  190. //----------------------------------------------------------------------------------------
  191.  
  192. FW_Boolean FW_HasStringByPosition(Environment *ev,
  193.                               FW_PResourceFile &file,
  194.                               FW_ResourceId resourceId,
  195.                               FW_ResourceType resourceType,
  196.                               unsigned short position)
  197. {
  198.     FW_PResource resource(ev, file, resourceId, resourceType);
  199.     FW_CAcquireResourceData data(ev, resource);
  200.     void *p = data.GetData();
  201.     
  202.     FW_SMultiStringHeader* head = (FW_SMultiStringHeader*) p;
  203.  
  204.     FW_ASSERT(position >= 0);
  205.     FW_ASSERT(head->fStringCount > 0);
  206.     
  207.     return position < head->fStringCount;
  208. }
  209.  
  210. //----------------------------------------------------------------------------------------
  211. // FW_LoadStringByPosition
  212. //----------------------------------------------------------------------------------------
  213.  
  214. FW_Boolean FW_HasStringByID(Environment *ev,
  215.                               FW_PResourceFile &file,
  216.                               FW_ResourceId resourceId,
  217.                               FW_ResourceType resourceType,
  218.                               unsigned short id)
  219. {
  220.     FW_PResource resource(ev, file, resourceId, resourceType);
  221.     FW_CAcquireResourceData data(ev, resource);
  222.     void *p = data.GetData();
  223.     
  224.     FW_SMultiStringHeader* head = (FW_SMultiStringHeader*) p;
  225.     FW_ASSERT(head->fStringCount > 0);
  226.     
  227.     // Compute the Offset into the table, based on the position.
  228.     const unsigned short offset = head->GetStringOffset(id);
  229.     return (offset != kBadOffset);
  230. }
  231.  
  232. //----------------------------------------------------------------------------------------
  233. // FW_LoadStringByPosition
  234. //----------------------------------------------------------------------------------------
  235.  
  236. void FW_LoadStringByPosition(Environment *ev,
  237.                               FW_PResourceFile &file,
  238.                               FW_ResourceId resourceId,
  239.                               FW_ResourceType resourceType,
  240.                               unsigned short position,
  241.                               FW_CString &string)
  242. {
  243.     FW_PResource resource(ev, file, resourceId, resourceType);
  244.     FW_CAcquireResourceData data(ev, resource);
  245.     void *p = data.GetData();
  246.     
  247.     FW_SMultiStringHeader* head = (FW_SMultiStringHeader*) p;
  248.     FW_ASSERT(head->fStringCount > 0);
  249.     
  250.     // Compute the Offset into the table, based on the position.
  251.     const unsigned short offset = head->fEntryArray[position].fStringOffset;
  252.     
  253.     string.ReplaceAll(((const FW_Char*)p)+offset);
  254. }
  255.  
  256. //----------------------------------------------------------------------------------------
  257. // FW_LoadStringByIDNoFail
  258. //----------------------------------------------------------------------------------------
  259.  
  260. FW_Boolean FW_LoadStringByIDNoFail(Environment *ev,
  261.                                 FW_PResourceFile &file,
  262.                                 FW_ResourceId resourceId,
  263.                                 FW_ResourceType resourceType,
  264.                                 unsigned short id,
  265.                                 FW_CString &string)
  266. {
  267.     FW_Boolean success = TRUE;
  268.     
  269.     FW_TRY
  270.     {
  271.         ::FW_LoadStringByID(ev, file, resourceId, resourceType, id, string);
  272.     }
  273.     FW_CATCH_BEGIN
  274.     FW_CATCH_EVERYTHING()
  275.     {
  276.         string.Truncate(0);
  277.         success = FALSE;
  278.     }
  279.     FW_CATCH_END
  280.  
  281.     return success;
  282. }
  283.  
  284. //----------------------------------------------------------------------------------------
  285. // FW_LoadStringByID
  286. //----------------------------------------------------------------------------------------
  287.                     
  288. void FW_LoadStringByID(Environment *ev,
  289.                         FW_PResourceFile &file,
  290.                         FW_ResourceId resourceId,
  291.                         FW_ResourceType resourceType,
  292.                         unsigned short id,
  293.                         FW_CString &string)
  294. {
  295.     FW_PResource resource(ev, file, resourceId, resourceType);
  296.     FW_CAcquireResourceData data(ev, resource);
  297.     void *p = data.GetData();
  298.     
  299.     FW_SMultiStringHeader* head = (FW_SMultiStringHeader*) p;
  300.     FW_ASSERT(head->fStringCount > 0);
  301.  
  302.     // Compute the Offset into the table, based on the position.
  303.     const unsigned short offset = head->GetStringOffset(id);
  304.     if (offset == kBadOffset)
  305.         FW_Failure(FW_xStringResourceNotFound);
  306.  
  307.     string.ReplaceAll(((const FW_Char*)p)+offset);
  308. }
  309.                     
  310.